package com.zhsj.OkeHomeConsumption.utils;

import android.app.Activity;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;

import com.zhsj.OkeHomeConsumption.empty.SelectCityEnity;

import java.util.ArrayList;
import java.util.List;

import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;


/**
 * @描述: 省-市-区县-乡镇 自定义滚动选择器
 * @创建人：zhangpeisen
 * @创建时间：2017/7/3 下午10:08
 * @修改人：zhangpeisen
 * @修改时间：2017/7/3 下午10:08
 * @修改备注：
 * @throws
 */

public class SelectCityPicker extends WheelPicker {

    private OnCitySelectListener mOnCitySelectListener;

    private Activity mContext;

    private ArrayList<SelectCityEnity> mSelectProviceEnities;

    private WheelView provinceView;
    private WheelView cityView;
    private WheelView countyView;
    private WheelView townView;

    public SelectCityPicker(Activity activity, ArrayList<SelectCityEnity> selectProviceEnities) {
        super(activity);
        mContext = activity;
        mSelectProviceEnities = selectProviceEnities;
    }


    private void onProvinceWheelRoll(int position) {
        cityView.setItems(getCityNames(position));
        countyView.setItems(getCountyNames(position, 0));
        townView.setItems(getTownNames(position, 0, 0));
    }

    private void onCityWheelRoll(int position) {
        countyView.setItems(getCountyNames(provinceView.getSelectedIndex(), position));
        townView.setItems(getTownNames(provinceView.getSelectedIndex(), position, 0));
    }

    private void onTownWheelRoll(int position) {
        townView.setItems(getTownNames(provinceView.getSelectedIndex(), cityView.getSelectedIndex(), position));
    }

    /**
     * 获取省份名称列表
     *
     * @return
     */
    private List<String> getProvinceNames() {
        List<String> provinces = new ArrayList<>();
        for (SelectCityEnity province : mSelectProviceEnities) {
            if (province == null) {
                provinces.add("");
            } else {
                provinces.add(province.getName());
            }
        }
        return provinces;
    }

    /**
     * 获取某个省份的城市名称列表
     *
     * @param provincePos
     * @return
     */
    private List<String> getCityNames(int provincePos) {
        if (provincePos == mSelectProviceEnities.size()) {
            provincePos = mSelectProviceEnities.size() - 1;
        }
        List<String> cities = new ArrayList<>();
        for (SelectCityEnity city : mSelectProviceEnities.get(provincePos).getChildren()) {
            if (city == null) {
                cities.add("");
            } else {
                cities.add(city.getName());
            }
        }
        return cities;
    }

    /**
     * 获取某个城市的县级区域名称列表
     *
     * @param provincePos
     * @param cityPos
     * @return
     */
    private List<String> getCountyNames(int provincePos, int cityPos) {
        if (provincePos == mSelectProviceEnities.size()) {
            provincePos = mSelectProviceEnities.size() - 1;
        }
        List<String> counties = new ArrayList<>();
        if (mSelectProviceEnities.get(provincePos).getChildren().size() > 0) {
            for (SelectCityEnity county : mSelectProviceEnities.get(provincePos).
                    getChildren().get(cityPos).getChildren()) {

                counties.add(county.getName());
            }
        }
        return counties;
    }

    /**
     * 获取某个城市的乡镇名称列表
     *
     * @param provincePos
     * @param cityPos
     * @return
     */
    private List<String> getTownNames(int provincePos, int cityPos, int countryPos) {
        if (provincePos == mSelectProviceEnities.size()) {
            provincePos = mSelectProviceEnities.size() - 1;
        }
        List<String> townties = new ArrayList<>();
        if (mSelectProviceEnities.get(provincePos).getChildren().isEmpty()) {
            townties.add("");
            return townties;
        }
        if (mSelectProviceEnities.get(provincePos).getChildren().get(cityPos).getChildren().size() > 0) {
            for (SelectCityEnity town : mSelectProviceEnities.get(provincePos).
                    getChildren().get(cityPos).getChildren().get(countryPos).getChildren()) {
                if (town == null) {
                    townties.add("");
                } else {
                    townties.add(town.getName());
                }
            }
        }
        return townties;
    }


    public SelectCityPicker setOnCitySelectListener(OnCitySelectListener listener) {
        mOnCitySelectListener = listener;
        return this;
    }


    @NonNull
    @Override
    protected View makeCenterView() {
        //初始化选择器
        if (mSelectProviceEnities.size() == 0) {
            throw new IllegalArgumentException("please initial options at first, can't be empty");
        }
        LinearLayout layout = new LinearLayout(activity);
        layout.setOrientation(LinearLayout.HORIZONTAL);
        layout.setGravity(Gravity.CENTER);

        // 省
        provinceView = new WheelView(activity);
        provinceView.setLayoutParams(new LinearLayout.LayoutParams(screenWidthPixels / 4, WRAP_CONTENT));
        provinceView.setTextSize(textSize);
        provinceView.setTextColor(textColorNormal, Color.parseColor("#536DFE"));
        provinceView.setLineVisible(lineVisible);
        provinceView.setLineColor(Color.parseColor("#536DFE"));
        provinceView.setOffset(offset);
        layout.addView(provinceView);
        // 市
        cityView = new WheelView(activity);
        cityView.setLayoutParams(new LinearLayout.LayoutParams(screenWidthPixels / 4, WRAP_CONTENT));
        cityView.setTextSize(textSize);
        cityView.setTextColor(textColorNormal, Color.parseColor("#536DFE"));
        cityView.setLineVisible(lineVisible);
        cityView.setLineColor(Color.parseColor("#536DFE"));
        cityView.setOffset(offset);
        layout.addView(cityView);
        // 县区
        countyView = new WheelView(activity);
        countyView.setLayoutParams(new LinearLayout.LayoutParams(screenWidthPixels / 4, WRAP_CONTENT));
        countyView.setTextSize(textSize);
        countyView.setTextColor(textColorNormal, Color.parseColor("#536DFE"));
        countyView.setLineVisible(lineVisible);
        countyView.setLineColor(Color.parseColor("#536DFE"));
        countyView.setOffset(offset);
        layout.addView(countyView);
        // 镇乡
        townView = new WheelView(activity);
        townView.setLayoutParams(new LinearLayout.LayoutParams(screenWidthPixels / 4, WRAP_CONTENT));
        townView.setTextSize(textSize);
        townView.setTextColor(textColorNormal, Color.parseColor("#536DFE"));
        townView.setLineVisible(lineVisible);
        townView.setLineColor(Color.parseColor("#536DFE"));
        townView.setOffset(offset);
        layout.addView(townView);
        provinceView.setItems(getProvinceNames(), provinceView.getSelectedIndex());
        cityView.setItems(getCityNames(0));
        countyView.setItems(getCountyNames(0, 0));
        townView.setItems(getTownNames(0, 0, 0));
        provinceView.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(boolean isUserScroll, int selectedIndex, String item) {
                onProvinceWheelRoll(selectedIndex);
            }
        });
        cityView.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(boolean isUserScroll, int selectedIndex, String item) {
                onCityWheelRoll(selectedIndex);
            }
        });
        countyView.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(boolean isUserScroll, int selectedIndex, String item) {
                onTownWheelRoll(selectedIndex);
            }
        });
        return layout;
    }

    @Override
    protected void onSubmit() {
        super.onSubmit();
        if (mOnCitySelectListener != null) {
            // 省
            SelectCityEnity province = mSelectProviceEnities.get(mSelectProviceEnities.size() == provinceView.getSelectedIndex()
                    ? provinceView.getSelectedIndex() - 1 : provinceView.getSelectedIndex());
            // 市
            SelectCityEnity city = province.getChildren().size() > 0 ? province.getChildren().get(cityView.getSelectedIndex()) : null;
            String provinceName = province.getName();
            int provinceId = province.getId();
            if (province.getChildren().isEmpty()) {
                mOnCitySelectListener.onCitySelect(provinceName, "", "", "", provinceId, 0, 0, 0);
                return;
            }
            String cityName = city == null ? "" : city.getName();
            int cityId = city == null ? 0 : city.getId();
            if (city.getChildren().isEmpty()) {
                mOnCitySelectListener.onCitySelect(provinceName, cityName, "", "", provinceId, cityId, 0, 0);
                return;
            }
            // 县级
            SelectCityEnity coutry = province.getChildren().get(cityView.getSelectedIndex()).getChildren().size() > 0 ?
                    province.getChildren().get(cityView.getSelectedIndex()).getChildren().get(countyView.getSelectedIndex()) : null;
            String countyName = city == null ? "" : city.getChildren().get(countyView.getSelectedIndex()).getName();
            int countyId = city == null ? 0 : city.getChildren().get(countyView.getSelectedIndex()).getId();
            if (coutry.getChildren().isEmpty()) {
                mOnCitySelectListener.onCitySelect(provinceName, cityName, countyName, "", provinceId, cityId, countyId, 0);
                return;
            }
            String townName = coutry == null ? "" : coutry.getChildren().get(townView.getSelectedIndex()).getName();
            int townId = coutry == null ? 0 : coutry.getChildren().get(townView.getSelectedIndex()).getId();
            mOnCitySelectListener.onCitySelect(provinceName, cityName, countyName, townName, provinceId, cityId, countyId, townId);
        }

    }

    public interface OnCitySelectListener {
        void onCitySelect(String province, String city, String county, String town, int provinceid, int cityid, int coutryid, int townid);
    }

}
